package com.FindMyStuff.Activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

import com.FindMyStuff.Model.InputChecker;
import com.FindMyStuff.Model.NullUser;
import com.FindMyStuff.Model.SessionManager;
import com.FindMyStuff.Model.User;

/**
 * A log in page
 *
 */
public class LogInActivity extends Activity {
	/**
	 * Holds the SessionManager so garbage collection will not get rid of the session manager
	 */
	private SessionManager sm;   

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		getActionBar().setDisplayShowHomeEnabled(false);
		getActionBar().setDisplayShowTitleEnabled(false);
		super.onCreate(savedInstanceState);
		sm = SessionManager.getInstance();
		
		
		
		setContentView(R.layout.activity_log_in);
	}
	
	/**
	 * Attempts to log a user in using the strings from the text boxes
	 * @param view
	 */
	public void tryLogin(View view)
	{
		Intent intent = new Intent(this, HomeActivity.class);
		EditText editEmail = (EditText) findViewById(R.id.edit_Email);
		EditText editPassword = (EditText) findViewById(R.id.edit_Password);
				
		String userEmail = editEmail.getText().toString();
		String userPassword = editPassword.getText().toString();
		
		//error holds the error strings for user and password
		String error = "";
		User user = new NullUser();
		//if email is invalid add email error
		//vague because it just checks for a regex match
		if(!InputChecker.checkEmail(userEmail))
		{
			error = "Email or Password is Incorrect";
		} else 
		{
			user = sm.attemptLogIn(this, userEmail, userPassword);
		}
		
		if(user.isLocked())
		{
			error = "Account is Locked:" +
					"You must wait for an Admin" +
					" to Unlock this Account Inorder to Login";
		} else if(user.isAdmin())
		{
			intent = new Intent(this, AdminHomeActivity.class);
		} 
		
		//called if email/pass errors and user has been
		//retrieved successfully from the database
		if(error.equals("") && user == sm.getCurrentUser())
		{
		    startActivity(intent);
		    //Calls animation
		    overridePendingTransition  (R.anim.right_slide_in, R.anim.right_slide_out);
		}
		else 
		{
			error = error.equals("") ? "Email or Password is Incorrect" : error;
			Dialog dialog = createIncorrectLogInDialog(error);
			dialog.show();
		}		
	}
	
	/**
	 * Opens the RegisterActivity page
	 * @param view
	 */
	public void register(View view)
	{
		Intent intent = new Intent(this, RegisterActivity.class);
		startActivity(intent);
		overridePendingTransition (R.anim.right_slide_in, R.anim.right_slide_out);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_log_in, menu);
		return true;
	}

	/**
	 * Creates a Dialog to display the errors found while
	 * checking the email and password
	 * 
	 * @param error The string containing all of the errors
	 * @return The Dialog object to be displayed
	 */
	
	public Dialog createIncorrectLogInDialog(String error) {
	    AlertDialog.Builder builder = new AlertDialog.Builder(this);

	    // Inflate and set the layout for the dialog
	    // Pass null as the parent view because its going in the dialog layout
	    builder.setMessage(error);
	    builder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
	               @Override
	               public void onClick(DialogInterface dialog, int id) {
	                   
	               }
	           });
	    return builder.create();
	}
}
